﻿using System.Collections.Generic;
using System.Xml.Linq;
using Harvest.API.Model;

namespace Harvest.API
{

    public partial class Wrapper
    {

        /// <summary>
        /// Get all users.
        /// </summary>
        /// <remarks>GET /people</remarks>
        /// <returns></returns>
        public List<User> GetUsers()
        {
            var data = Request(WebRequestMethodEnum.Get, "people");
            var xDocument = XDocument.Parse(data);
            return xDocument.ToUsers();
        }

        /// <summary>
        /// Get an existing user.
        /// </summary>
        /// <remarks>GET /people/#{user_id}</remarks>
        /// <param name="id"></param>
        /// <returns></returns>
        public User GetUser(int id)
        {
            var data = Request(WebRequestMethodEnum.Get, "people/" + id);
            var xDocument = XDocument.Parse(data);
            return xDocument.ToUser();
        }

        /// <summary>
        /// Delete an existing user.
        /// </summary>
        /// <remarks>DELETE /people/#{user_id}}</remarks>
        /// <param name="id"></param>
        /// <returns></returns>
        public string DeleteUser(int id)
        {
            var data = Request(WebRequestMethodEnum.Delete, "people/" + id);
            return data;
        }

        /// <summary>
        /// Activate or deactivate an existing user.
        /// </summary>
        /// <remarks>POST /people/#{user_id}/toggle</remarks>
        /// <param name="id"></param>
        /// <returns></returns>
        public string ToggleUserActive(int id)
        {
            var data = Request(WebRequestMethodEnum.Post, "people/" + id + "/toggle");
            return data;
        }

        /// <summary>
        /// Update an existing user.
        /// </summary>
        /// <remarks>PUT /people/#{user_id}</remarks>
        /// <param name="user"></param>
        /// <returns></returns>
        public string UpdateUser(User user)
        {
            var data = Request(WebRequestMethodEnum.Put, "people/" + user.Id, user.Serialize());
            return data;
        }

        /// <summary>
        /// Create a new user.
        /// </summary>
        /// <remarks>POST /people</remarks>
        /// <param name="user"></param>
        /// <returns></returns>
        public string CreateUser(User user)
        {
            var data = Request(WebRequestMethodEnum.Post, "people", user.Serialize());
            return data;
        }

        /// <summary>
        /// Resets password for  an existing user.
        /// </summary>
        /// <remarks>POST /people/#{user_id}/reset_password</remarks>
        /// <param name="id"></param>
        /// <returns></returns>
        public string ResetUserPassword(int id)
        {
            var data = Request(WebRequestMethodEnum.Post, "people/" + id + "/reset_password");
            return data;
        }

    }

}
